<?xml version="1.0" encoding="UTF-8"?>

<!-- 
 * Copyright 2008 Philippe Prados.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
-->

<?xml-stylesheet type="text/xsl" href="../../../../xslt/xslt-to-xhtml.xslt" ?>
<xsl:stylesheet
	version="2.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation=
		"http://www.w3.org/1999/XSL/Transform http://www.w3.org/2007/schema-for-xslt20.xsd
		 http://jndi-resources.googlecode.com/1.0/ http://www.prados.fr/xsd/1.0/jndi-resources.xsd
		"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:sch="http://www.w3.org/2001/XMLSchema"
	xmlns:jndi="http://jndi-resources.googlecode.com/1.0/"
	xmlns:tools="http://jndi-resources.googlecode.com/1.0/java/com.googlecode.jndiresources.tools.XSLTools"
	exclude-result-prefixes="#all"
>
<xsl:include href="../../install-drivers.xslt"/>


<xsl:variable name="lib"><xsl:value-of select="concat($targetdir,'jboss.server.conf/lib/')"/></xsl:variable>

<xsl:template match="text()|comment()" />

<xsl:template match="jndi:resources[@id=$currentid]">
<xsl:value-of select="tools:info(concat('Generate ',$filename))"/>
<xsl:result-document  href="{$filename}">
<xsl:text>&#xA;</xsl:text>
<xsl:comment>Generated by jndi-resources v<xsl:value-of select="$version"/></xsl:comment>
<xsl:text>&#xA;</xsl:text>
<xsl:processing-instruction name="jndi-stylesheet">href="xslt/save.xslt" target="<xsl:value-of select="concat($basefilename,'.xml')"/>"</xsl:processing-instruction>
<xsl:text>&#xA;</xsl:text>
<datasources>
	<xsl:apply-templates />
	<xsl:call-template name="footer"/>
</datasources>
</xsl:result-document>

</xsl:template>

<xsl:template match="jndi:resources[@id=$currentid]/jndi:resource[@familly=$familly]">

		<xsl:if test="jndi:property[(@name='xa' and @value='local')] or count(jndi:property[@name='xa'])=0">
			<xsl:text>&#xA;   </xsl:text>
			<xsl:text>&#xA;   </xsl:text>
			<xsl:comment>Register Datasource for key <xsl:value-of select="@name"/></xsl:comment>
			<xsl:text>&#xA;   </xsl:text>
			<local-tx-datasource>
			  	<jndi-name>${jndi-prefix}<xsl:value-of select="@name"/></jndi-name>
				<use-java-context><xsl:value-of select="$use-java-context"/></use-java-context>
			    <driver-class><xsl:value-of select="$jdbc.driver"/></driver-class>
			    <connection-url>
				    <xsl:choose>
				    	<xsl:when test=".//jndi:property[@name='username']">
				    		<xsl:value-of select=".//jndi:property[@name='connection-url']/@value"/>
				    	</xsl:when>
				    	<xsl:otherwise><xsl:value-of select="$jdbc.url.default"/></xsl:otherwise>
				    </xsl:choose>
				</connection-url>

				<xsl:call-template name="params"/>
				<xsl:copy-of select="$jdbc.addon"/>
				<xsl:copy-of select="jndi:extends[@appsrv=$appsrv]/*" copy-namespaces="no"/>
			</local-tx-datasource>
		</xsl:if>

		<xsl:if test="jndi:property[@name='xa' and @value='no']">
			<xsl:text>&#xA;   </xsl:text>
			<xsl:text>&#xA;   </xsl:text>
			<xsl:comment>Register Datasource for key <xsl:value-of select="@name"/></xsl:comment>
			<xsl:text>&#xA;   </xsl:text>
			<no-tx-datasource>
			  	<jndi-name>${jndi-prefix}<xsl:value-of select="@name"/></jndi-name>
				<use-java-context><xsl:value-of select="$use-java-context"/></use-java-context>
			    <driver-class><xsl:value-of select="$jdbc.driver"/></driver-class>
			    <connection-url>
				    <xsl:choose>
				    	<xsl:when test=".//jndi:property[@name='username']">
				    		<xsl:value-of select=".//jndi:property[@name='connection-url']/@value"/>
				    	</xsl:when>
				    	<xsl:otherwise><xsl:value-of select="$jdbc.url.default"/></xsl:otherwise>
				    </xsl:choose>
				</connection-url>

				<xsl:call-template name="params"/>
				<xsl:copy-of select="$jdbc.addon"/>
				<xsl:copy-of select="jndi:extends[@appsrv=$appsrv]/*" copy-namespaces="no"/>
			</no-tx-datasource>
		</xsl:if>

		<xsl:if test="jndi:property[@name='xa' and @value='yes']">
			<xsl:if test="$jdbc.xa.driver = ''">
				<xsl:value-of select="error(sch:QName('error9999'),concat('The database ',@familly,' is not compatible with XA.'))"/>
			</xsl:if>
			<xsl:text>&#xA;   </xsl:text>
			<xsl:text>&#xA;   </xsl:text>
			<xsl:comment>Register XA Datasource for key <xsl:value-of select="@name"/></xsl:comment>
			<xsl:text>&#xA;   </xsl:text>
			<xa-datasource>
				<jndi-name>${jndi-prefix}<xsl:value-of select="@name"/></jndi-name>
				<use-java-context><xsl:value-of select="$use-java-context"/></use-java-context>
			    <track-connection-by-tx>true</track-connection-by-tx>
			    <isSameRM-override-value>false</isSameRM-override-value>
			    <xa-datasource-class><xsl:value-of select="$jdbc.xa.driver"/></xa-datasource-class>
			    <xa-datasource-property name="URL">
				    <xsl:choose>
				    	<xsl:when test=".//jndi:property[@name='connection-url']">
				    		<xsl:value-of select=".//jndi:property[@name='connection-url']/@value"/>
				    	</xsl:when>
				    	<xsl:otherwise><xsl:value-of select="$jdbc.xa.url.default"/></xsl:otherwise>
				    </xsl:choose>
				</xa-datasource-property>
				<xsl:call-template name="params"/>
				<xsl:copy-of select="$jdbc.xa.addon"/>
				<xsl:copy-of select="jndi:extends[@appsrv=$appsrv]/*" copy-namespaces="no"/>
			</xa-datasource>
		</xsl:if>


</xsl:template>

<xsl:template name="params">
    <user-name>
	    <xsl:choose>
	    	<xsl:when test=".//jndi:property[@name = 'username']">
	    		<xsl:value-of select=".//jndi:property[@name = 'username']/@value"/>
	    	</xsl:when>
	    	<xsl:otherwise><xsl:value-of select="$jdbc.username"/></xsl:otherwise>
	    </xsl:choose>
    </user-name>
    <password>
    	<xsl:choose>
	    	<xsl:when test=".//jndi:property[@name = 'username']">
	    		<xsl:value-of select=".//jndi:property[@name = 'password']/@value"/>
	    	</xsl:when>
	    	<xsl:otherwise><xsl:value-of select="$jdbc.password"/></xsl:otherwise>
	    </xsl:choose>
    </password>
    <min-pool-size>
    	<xsl:choose>
	    	<xsl:when test=".//jndi:property[@name = 'min-pool-size']">
	    		<xsl:value-of select=".//jndi:property[@name = 'min-pool-size']/@value"/>
	    	</xsl:when>
	    	<xsl:otherwise>${jdbc.min-pool-size}</xsl:otherwise>
	    </xsl:choose>
	</min-pool-size>
    <max-pool-size>
    	<xsl:choose>
	    	<xsl:when test=".//jndi:property[@name = 'max-pool-size']">
	    		<xsl:value-of select=".//jndi:property[@name = 'max-pool-size']/@value"/>
	    	</xsl:when>
	    	<xsl:otherwise>${jdbc.max-pool-size}</xsl:otherwise>
	    </xsl:choose>
	</max-pool-size>
    <prepared-statement-cache-size>
    	<xsl:choose>
	    	<xsl:when test=".//jndi:property[@name = 'prepared-statement-cache-size']">
	    		<xsl:value-of select=".//jndi:property[@name = 'prepared-statement-cache-size']/@value"/>
	    	</xsl:when>
	    	<xsl:otherwise>${jdbc.prepared-statement-cache-size}</xsl:otherwise>
	    </xsl:choose>
	</prepared-statement-cache-size>
	<check-valid-connection-sql>
    	<xsl:choose>
	    	<xsl:when test=".//jndi:property[@name = 'validation-query']">
	    		<xsl:value-of select=".//jndi:property[@name = 'validation-query']/@value"/>
	    	</xsl:when>
	    	<xsl:otherwise><xsl:value-of select="$jdbc.validation-query"/></xsl:otherwise>
	    </xsl:choose>
	</check-valid-connection-sql>
	<query-timeout>
    	<xsl:choose>
	    	<xsl:when test=".//jndi:property[@name = 'max-wait']">
	    		<xsl:value-of select=".//jndi:property[@name = 'max-wait']/@value"/>
	    	</xsl:when>
	    	<xsl:otherwise>${jdbc.max-wait}</xsl:otherwise>
	    </xsl:choose>
	</query-timeout>
<!-- TODO : Specifique JBOSS ?-->
    <transaction-isolation>
    	<xsl:choose>
	    	<xsl:when test=".//jndi:property[@name = 'transaction-isolation']">
	    		<xsl:value-of select=".//jndi:property[@name = 'transaction-isolation']/@value"/>
	    	</xsl:when>
	    	<xsl:otherwise>${jdbc.transaction-isolation}</xsl:otherwise>
	    </xsl:choose>
    </transaction-isolation>
</xsl:template>

</xsl:stylesheet>
